package c;



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-27
 * Time: 9:07
 */
public class TcpEchoClient {
    private Socket socket = null;
    public TcpEchoClient(String serverIP , int serverPort) throws IOException {
        socket = new Socket(serverIP,serverPort);
    }
    public void start() throws IOException {
        System.out.println("客户端启动!");
        Scanner scanner = new Scanner(System.in);

            try(InputStream inputStream = socket.getInputStream();
                OutputStream outputStream = socket.getOutputStream()){
                while(true){
                    System.out.print("请输入要传输的数据:");
                Scanner scannerClient = new Scanner(inputStream);
                //从控制台获取数据
                String request = scanner.next();
                request += "\n";
                //把数据传输给服务器
                outputStream.write(request.getBytes());
                //从服务器上获取响应
                String response = scannerClient.next();
                //把数据写回到控制台上

                System.out.println(response);
            }



        }
    }
    public static void main(String[] args) throws IOException {
       TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1",9090);
       tcpEchoClient.start();

    }
}

